package com.dragon.datastructure.tree.heap;

/**
 * 最大堆的测试
 *
 * @author dragon
 * @since 2019/12/31
 */
public class MaxHeapTest {

    public static void main(String[] args) {
        int count = 1_00_000_000;
        Integer[] array = new Integer[count];
        for (int i = 0; i < count; i++) {
            array[i] = (int) (Math.random() * Integer.MAX_VALUE);
        }
        // 消耗的时间: 986 false
        testHeap(array, true);

        // 消耗的时间: 705 true
    }


    public static void testHeap(Integer[] testData, boolean isHeapify) {
        long startTime = System.currentTimeMillis();
        MaxHeap<Integer> maxHeap;
        if (isHeapify) {
            maxHeap = new MaxHeap<>(testData);
        } else {
            maxHeap = new MaxHeap<>();
            for (Integer num : testData) {
                maxHeap.add(num);
            }
        }

        int[] array = new int[testData.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = maxHeap.extractMax();
        }
        for (int i = 1; i < array.length; i++) {
            if (array[i - 1] < array[i]) {
                throw new RuntimeException("Error");
            }
        }

        long endTime = System.currentTimeMillis();

        System.out.println("消耗的时间: " + (endTime - startTime));
    }
}
